package ncme.auto.framework.core.logCore.util;

import ncme.auto.framework.core.entities.ResMsg;
import ncme.auto.framework.core.logCore.entity.RequestLog;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;

public class RequestUtil {

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    public static RequestLog getRequestInfo(HttpServletRequest request, String requestMethod, Object[] args, Object result, long requestTime) {
        RequestLog requestLog = new RequestLog();
        //请求日志
        String header = request.getHeader("User-Agent");
        requestLog.setRequestTime(new Date());
        requestLog.setHostIP(RequestUtil.getIpAddr(request));
        requestLog.setHostLocation("暂时无法获取");
        requestLog.setRequestURL(request.getRequestURL().toString());
        requestLog.setRequestType(request.getMethod());
        requestLog.setRequestMethodName(requestMethod);
        requestLog.setRequestParam(Arrays.toString(args));
        requestLog.setResponseParam(result == null ? null : result.toString());
        if (result instanceof ResMsg) {
            ResMsg res = (ResMsg) result;
            requestLog.setResponseStatus(res.getState());
        } else
            requestLog.setResponseStatus("no state");
        requestLog.setRequestPlatform(RequestUtil.getUserAgent(header).getUserPlatform());
        requestLog.setRequestBrowser(RequestUtil.getUserAgent(header).getUserBrowser());
        requestLog.setExecuteTime(System.currentTimeMillis() - requestTime);
        return requestLog;
    }

    public static String getIpAddr0(HttpServletRequest request) {
        String ip = "";
        try {
            ip = request.getHeader("X-Forwarded-For");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("Proxy-Client-IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("WL-Proxy-Client-IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_CLIENT_IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getRemoteAddr();
                }
            } else if (ip.length() > 15) {
                String[] ips = ip.split(",");
                for (int index = 0; index < ips.length; index++) {
                    String strIp = (String) ips[index];
                    if (!("unknown".equalsIgnoreCase(strIp))) {
                        ip = strIp;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            //logger.error("Error",e);
        }
        return ip;
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String string = headerNames.nextElement();
            System.out.println(string + " = " + request.getHeader(string));
        }

        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


    public static class Visit {
        public Visit(String userBrowser, String userPlatform) {
            this.userBrowser = userBrowser;
            this.userPlatform = userPlatform;
        }

        public String getUserBrowser() {
            return userBrowser;
        }

        public void setUserBrowser(String userBrowser) {
            this.userBrowser = userBrowser;
        }

        public String getUserPlatform() {
            return userPlatform;
        }

        public void setUserPlatform(String userPlatform) {
            this.userPlatform = userPlatform;
        }

        private String userBrowser;
        private String userPlatform;
    }

    public static Visit getUserAgent(String userAgent) {
        if (userAgent == "" || userAgent == null) {
            userAgent = "";
        }
        if (userAgent.contains("Windows")) {
            if (userAgent.contains("Windows NT 10.0")) {
                return judgeBrowser(userAgent, "Windows 10");
            } else if (userAgent.contains("Windows NT 6.2")) {
                return judgeBrowser(userAgent, "Windows 8");
            } else if (userAgent.contains("Windows NT 6.1")) {
                return judgeBrowser(userAgent, "Windows 7");
            } else if (userAgent.contains("Windows NT 6.0")) {
                return judgeBrowser(userAgent, "Windows Vista");
            } else if (userAgent.contains("Windows NT 5.2")) {
                return judgeBrowser(userAgent, "Windows XP");
            } else if (userAgent.contains("Windows NT 5.1")) {
                return judgeBrowser(userAgent, "Windows XP");
            } else if (userAgent.contains("Windows NT 5.01")) {
                return judgeBrowser(userAgent, "Windows 2000");
            } else if (userAgent.contains("Windows NT 5.0")) {
                return judgeBrowser(userAgent, "Windows 2000");
            } else if (userAgent.contains("Windows NT 4.0")) {
                return judgeBrowser(userAgent, "Windows NT 4.0");
            } else if (userAgent.contains("Windows 98; Win 9x 4.90")) {
                return judgeBrowser(userAgent, "Windows ME");
            } else if (userAgent.contains("Windows 98")) {
                return judgeBrowser(userAgent, "Windows 98");
            } else if (userAgent.contains("Windows 95")) {
                return judgeBrowser(userAgent, "Windows 95");
            } else if (userAgent.contains("Windows CE")) {
                return judgeBrowser(userAgent, "Windows CE");
            }
        } else if (userAgent.contains("Mac OS X")) {
            if (userAgent.contains("iPhone")) {
                return judgeBrowser(userAgent, "iPhone");
            } else if (userAgent.contains("iPad")) {
                return judgeBrowser(userAgent, "iPad");//判断系统
            } else {
                return judgeBrowser(userAgent, "Mac");//判断系统
            }
        } else if (userAgent.contains("Android")) {
            return judgeBrowser(userAgent, "Android");//判断系统
        } else if (userAgent.contains("Linux")) {
            return judgeBrowser(userAgent, "Linux");//判断系统
        } else if (userAgent.contains("FreeBSD")) {
            return judgeBrowser(userAgent, "FreeBSD");//判断系统
        } else if (userAgent.contains("Solaris")) {
            return judgeBrowser(userAgent, "Solaris");//判断系统
        }
        return judgeBrowser(userAgent, "其他");
    }

    private static Visit judgeBrowser(String userAgent, String platformType) {
        if (userAgent.contains("Edge")) {
            return new Visit("Microsoft Edge", platformType);
        } else if (userAgent.contains("QQBrowser")) {
            return new Visit("腾讯浏览器", platformType);
        } else if (userAgent.contains("Chrome") && userAgent.contains("Safari")) {
            return new Visit("Chrome", platformType);
        } else if (userAgent.contains("Firefox")) {
            return new Visit("Firefox", platformType);
        } else if (userAgent.contains("360")) {//Internet Explorer 6
            return new Visit("360浏览器", platformType);
        } else if (userAgent.contains("Opera")) {//Internet Explorer 6
            return new Visit("Opera", platformType);
        } else if (userAgent.contains("Safari") && !userAgent.contains("Chrome")) {
            return new Visit("Safari", platformType);
        } else if (userAgent.contains("MetaSr1.0")) {//Internet Explorer 6
            return new Visit("搜狗浏览器", platformType);
        } else if (userAgent.contains("TencentTraveler")) {//Internet Explorer 6
            return new Visit("腾讯浏览器", platformType);
        } else if (userAgent.contains("UC")) {//Internet Explorer 6
            return new Visit("UC浏览器", platformType);
        } else if (userAgent.contains("MSIE")) {
            if (userAgent.contains("MSIE 10.0")) {//Internet Explorer 10
                return new Visit("IE 10", platformType);
            } else if (userAgent.contains("MSIE 9.0")) {//Internet Explorer 9
                return new Visit("IE 9", platformType);
            } else if (userAgent.contains("MSIE 8.0")) {//Internet Explorer 8
                return new Visit("IE 8", platformType);
            } else if (userAgent.contains("MSIE 7.0")) {//Internet Explorer 7
                return new Visit("IE 7", platformType);
            } else if (userAgent.contains("MSIE 6.0")) {//Internet Explorer 6
                return new Visit("IE 6", platformType);
            }
        } else {
            return new Visit("其他", platformType);
        }
        return new Visit("其他", platformType);
    }

}
